001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------
028 * ColorBlock.java
029 * ---------------
030 * (C) Copyright 2004-2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes:
036 * --------
037 * 22-Oct-2004 : Version 1 (DG);
038 * 20-Apr-2005 : Added new draw() method (DG);
039 * ------------- JFREECHART 1.0.x ---------------------------------------------
040 * 16-Mar-2007 : Implemented equals() and fixed serialization (DG);
041 * 08-Apr-2008 : Added code for margin, border and padding in draw()
042 * method (DG);
043 *
044 */
045
046 package org.jfree.chart.block;
047
048 import java.awt.Graphics2D;
049 import java.awt.Paint;
050 import java.awt.geom.Rectangle2D;
051 import java.io.IOException;
052 import java.io.ObjectInputStream;
053 import java.io.ObjectOutputStream;
054
055 import org.jfree.io.SerialUtilities;
056 import org.jfree.ui.Size2D;
057 import org.jfree.util.PaintUtilities;
058
059 /**
060 * A block that is filled with a single color.
061 */
062 public class ColorBlock extends AbstractBlock implements Block {
063
064 /** For serialization. */
065 static final long serialVersionUID = 3383866145634010865L;
066
067 /** The paint. */
068 private transient Paint paint;
069
070 /**
071 * Creates a new block.
072 *
073 * @param paint the paint (<code>null</code> not permitted).
074 * @param width the width.
075 * @param height the height.
076 */
077 public ColorBlock(Paint paint, double width, double height) {
078 if (paint == null) {
079 throw new IllegalArgumentException("Null 'paint' argument.");
080 }
081 this.paint = paint;
082 setWidth(width);
083 setHeight(height);
084 }
085
086 /**
087 * Returns the paint.
088 *
089 * @return The paint (never <code>null</code>).
090 *
091 * @since 1.0.5
092 */
093 public Paint getPaint() {
094 return this.paint;
095 }
096
097 /**
098 * Arranges the contents of the block, within the given constraints, and
099 * returns the block size.
100 *
101 * @param g2 the graphics device.
102 * @param constraint the constraint (<code>null</code> not permitted).
103 *
104 * @return The block size (in Java2D units, never <code>null</code>).
105 */
106 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
107 return new Size2D(calculateTotalWidth(getWidth()),
108 calculateTotalHeight(getHeight()));
109 }
110
111 /**
112 * Draws the block.
113 *
114 * @param g2 the graphics device.
115 * @param area the area.
116 */
117 public void draw(Graphics2D g2, Rectangle2D area) {
118 area = trimMargin(area);
119 drawBorder(g2, area);
120 area = trimBorder(area);
121 area = trimPadding(area);
122 g2.setPaint(this.paint);
123 g2.fill(area);
124 }
125
126 /**
127 * Draws the block within the specified area.
128 *
129 * @param g2 the graphics device.
130 * @param area the area.
131 * @param params ignored (<code>null</code> permitted).
132 *
133 * @return Always <code>null</code>.
134 */
135 public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
136 draw(g2, area);
137 return null;
138 }
139
140 /**
141 * Tests this block for equality with an arbitrary object.
142 *
143 * @param obj the object (<code>null</code> permitted).
144 *
145 * @return A boolean.
146 */
147 public boolean equals(Object obj) {
148 if (obj == this) {
149 return true;
150 }
151 if (!(obj instanceof ColorBlock)) {
152 return false;
153 }
154 ColorBlock that = (ColorBlock) obj;
155 if (!PaintUtilities.equal(this.paint, that.paint)) {
156 return false;
157 }
158 return super.equals(obj);
159 }
160
161 /**
162 * Provides serialization support.
163 *
164 * @param stream the output stream.
165 *
166 * @throws IOException if there is an I/O error.
167 */
168 private void writeObject(ObjectOutputStream stream) throws IOException {
169 stream.defaultWriteObject();
170 SerialUtilities.writePaint(this.paint, stream);
171 }
172
173 /**
174 * Provides serialization support.
175 *
176 * @param stream the input stream.
177 *
178 * @throws IOException if there is an I/O error.
179 * @throws ClassNotFoundException if there is a classpath problem.
180 */
181 private void readObject(ObjectInputStream stream)
182 throws IOException, ClassNotFoundException {
183 stream.defaultReadObject();
184 this.paint = SerialUtilities.readPaint(stream);
185 }
186
187 }